import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import mplfinance as mpf
import matplotlib.dates as mdates
import datetime as dt
import plotly.graph_objects as go
import plotly.express as px
import plotly.io as pio
from plotly.subplots import make_subplots
pio.renderers.default = "notebook"
pio.templates.default = "plotly_dark"
import gc
np.random.seed(42)
import warnings
warnings.filterwarnings("ignore")
To hedge, in finance, is to take an offsetting position in an asset or investment that reduces the price risk of an existing position. A hedge is therefore a trade that is made with the purpose of reducing the risk of adverse price movements in another asset. Normally, a hedge consists of taking the opposite position in a related security or in a derivative security based on the asset to be hedged.
The spot market for any thing is that the market for immediate delivery. You pay the money, you get the item. Forward and futures reflect contracts involving future delivery.
A forward contract is a contract between two individuals, you might call them counter-parties, to deliver at a future date called the exercise date or maturity date. Historically, forward contracts preceded futures contracts.
If you're going to go with forwards, you have to worry about your counterparty, and you have to check the counterparty out, and you need to establish that the counterparty has good credit so they're inherently limited.
A forward exchange contract, FX forwards can be thought of as a pair of zero coupon bonds stapled together in two different currencies.
Let's have a look at an example: Suppose I'm selling goods. I'm a Japanese producer of goods which I'm selling in the United States. I'm going to sell them for dollars of course, Americans pay dollars. And then I want to repatriate the money to Japan, so I'm going to exchange it into yen. But I want to have a forward contract which locks in the price today. I don't have to go through the forward market. I can borrow the money today in dollars, exchange it at the spot exchange rate today into yen, take the yen back to Japan and put them in some interest bearing account in Japan earning the Japanese interest rate. So I'm going to be charged the dollar interest rate but I'm going to be credited the Japanese interest rate. And so I have an amount left at the end equal to:
$$ \text{Forward Rate} = \frac{1 + \text{Japanese Interest Rate}}{1 + \text{Dollar Interest Rate}} \times \text{Spot Rate} $$So, ideally, forward rate should be equal to the expression above.
The forward rate agreement is given by:
$$ \text{Settlement} = \frac{(L-R)\times D\times A}{(B\times 100)+L\times D} $$where
A futures contract is like a forward contract except there's an intermediary. The exchange who, you make your contract with the intermediary, not with the ultimate counter party, and they're standardized.
Future works on the concept of margin. If market goes agains you, money is deducted from your margin account. This means that you can close away from the contract anytime you want, the money is deducted from your margin account.
There are two types of margins:
A margin call is a request for additional funds from a customer to meet a margin requirement. The margin call is made by the broker to the customer. The customer has to deposit the funds with the broker. The broker then uses the funds to meet the margin call.
Future contracts are not usually delivered. It's just an insurance against price drops.
Contango is a situation in which the futures price is higher than the spot price. This is because the futures price is the price of the underlying asset at a future date, and the spot price is the price of the underlying asset at the current date. The difference between the two prices is the interest rate.
As time increases, the futures price increases more than the spot price. Meaning that the slope is positive. This is contango.
The futures market makes the shortages less severe because you can see it coming in the futures curve.
When someone "buys" a futures contract, they are agreeing to buy the underlying asset at a future date at a price that is set today. When someone "sells" a futures contract, they are agreeing to sell the underlying asset at a future date at a price that is set today.
What actually happens is that a buyer agrees with the exchange to a daily settlement procedure. This is done by the initial margin give by the buyer.
Every day until expiration, a margin account is credited or debited with an amount equal to the change in the settle price times the contract amount. The amount credited or debited is called the daily settlement amount and is calculated as follows:
$$ \text{Daily Settlement Amount} = \text{Settle Price} \times \text{Contract Amount} $$Last Day: The last day is different. The buyer/seller has to pay the difference between the settle price and the spot price times the contract amount. (Or else they have to be ready with their truck to deliver the goods /take goods).
This means that you don't need to buy/sell the underlying asset. You just need to have the money to pay the difference between the settle price and the spot price.
On the last day, the spot price is going to be approximately equal to the futures price.
Arbitrage is the simultaneous purchase and sale of the same or similar asset in different markets in order to profit from tiny differences in the asset's listed price. It exploits short-lived variations in the price of identical or similar financial instruments in different markets or in different forms.
Fair Value In Futures Contract is a little bit like the forward interest parity in forward rate. This answers the question: What should the futures price be? We have:
$$ P_{\text{Future}} = P_{\text{Spot}} \times (1+r+s) $$where
The contango can be easily explained by above formula.
It is possible that the future price is less than the spot price. This is called backwardation.This can happen when the commodity is not in storage escpecially at harvest time.
There is no obvious spot oil price. Now there are spot oil prices but it's overwhelming majority of oil is not sold on the spot market. The spot oil price is a volatile number, it differs from place to place. Again, when you look at oil price typically when they quote that, they're quoting the nearest future contract.
SPI stands for stock price index. In US, S&P 500 is used. The future price is given by:
$$ F = P + P\times(r-y) $$where
The formula is same as the fair price for a commodity with a negative storage cost. The storage cost is negative because storing stocks gives you a dividend yield.
As an index can not be deliverable, cash settlement is used. The lot size varies from exchange to exchange. In US, it is 250.
There are two types of options:
At the time that the option is bought, the buyer of the option compensates the seller, the writer, of the option. Because the writer of the option is now subject to the choice in the future of the buyer of the option. And it has to be compensated for giving that choice to the other person.
With a call option, you are buying the right to buy something which is specified in the contract at a predetermined price. That means you don't have to wait to find out what your market price is of that item on the day of purchase. It's locked in in advance. But we call it an option because you don't have to purchase it, you can wait and see.
You will buy a call option on the date specified if the price of buying it through the option is less than the price of buying it in the open market. Because you will always do that, even if you decide you don't want the object, because you can then sell it in the open market and make a profit.
With a put option, you are buying the right to sell something which is specified in the contract at a predetermined price.
You will buy a put option on the date specified if the price of selling it through the option is more than the price of selling it in the open market. Because you will always do that, even if you decide you don't want to sell it, because you can then buy it in the open market and make a profit.
Options are optional, futures are not. Futures are binding. You have to buy or sell the underlying asset. Options are not binding. You can choose not to buy or sell the underlying asset.
There are three main terms of options:
Bid price is the price at which the option can be sold. Ask price is the price at which the option can be bought. Last price is the price at which the option was last traded.
Theoretical and Behavioral reasons.
On expiry date, you exercise a put option only if the spot price is less than the exercise price. You exercise a call option only if the spot price is more than the exercise price. So, the value of a put option is equal to the value of a call option plus the difference between the spot price and the exercise price.
Let's plot how return varies on the excersice date for a put and call option.
# a function to calculate the return if you excersice the put on last day
def put_return(spot_price, strike_price, type="long", premium=0):
if type == "long":
return max(strike_price - spot_price, 0) - premium
elif type == "short":
return min(spot_price - strike_price, 0) + premium
# a function to calculate the return if you excersice the call on last day
def call_return(spot_price, strike_price, type="long", premium=0):
if type == "long":
return max(spot_price - strike_price, 0) - premium
elif type == "short":
return min(strike_price - spot_price, 0) + premium
strike_price = 100
premium = 5
spot_prices = np.arange(80, 120, 0.1)
put_return_long = [
put_return(spot_price, strike_price, type="long", premium=premium)
for spot_price in spot_prices
]
put_return_short = [
put_return(spot_price, strike_price, type="short", premium=premium)
for spot_price in spot_prices
]
call_return_long = [
call_return(spot_price, strike_price, type="long", premium=premium)
for spot_price in spot_prices
]
call_return_short = [
call_return(spot_price, strike_price, type="short", premium=premium)
for spot_price in spot_prices
]
put_return_long = np.array(put_return_long)
put_return_short = np.array(put_return_short)
call_return_long = np.array(call_return_long)
call_return_short = np.array(call_return_short)
# Calculate_break_even points
# Put
put_break_even_long = strike_price - premium
put_break_even_short = strike_price - premium
# Call
call_break_even_long = strike_price + premium
call_break_even_short = strike_price + premium
figure = make_subplots(
rows=2, cols=2, subplot_titles=("Long Put", "Short Put", "Long Call", "Short Call")
)
figure.add_trace(
go.Scatter(x=spot_prices, y=put_return_long, name="Long Put"), row=1, col=1
)
figure.add_trace(
go.Scatter(x=spot_prices, y=put_return_short, name="Short Put"), row=1, col=2
)
figure.add_trace(
go.Scatter(x=spot_prices, y=call_return_long, name="Long Call"), row=2, col=1
)
figure.add_trace(
go.Scatter(x=spot_prices, y=call_return_short, name="Short Call"), row=2, col=2
)
# Adding breakeven point as circle
figure.add_trace(
go.Scatter(
x=[put_break_even_long],
y=[0],
mode="markers",
marker=dict(size=10, color="red"),
name="Breakeven for Long Put",
),
row=1,
col=1,
)
figure.add_trace(
go.Scatter(
x=[put_break_even_short],
y=[0],
mode="markers",
marker=dict(size=10, color="red"),
name="Breakeven for Short Put",
),
row=1,
col=2,
)
figure.add_trace(
go.Scatter(
x=[call_break_even_long],
y=[0],
mode="markers",
marker=dict(size=10, color="red"),
name="Breakeven for Long Call",
),
row=2,
col=1,
)
figure.add_trace(
go.Scatter(
x=[call_break_even_short],
y=[0],
mode="markers",
marker=dict(size=10, color="red"),
name="Breakeven for Short Call",
),
row=2,
col=2,
)
figure.update_layout(
title="Options Payoffs",
xaxis_title="Spot Price",
yaxis_title="Return",
legend_title="Option Type",
font=dict(family="Courier New, monospace", size=18, color="#7f7f7f"),
)
# Setting y axis range
figure.update_yaxes(range=[-premium - 10, premium + 10], row=1, col=1)
figure.update_yaxes(range=[-premium - 10, premium + 10], row=1, col=2)
figure.update_yaxes(range=[-premium - 10, premium + 10], row=2, col=1)
figure.update_yaxes(range=[-premium - 10, premium + 10], row=2, col=2)
figure.show()
# Breakevens for put+call
put_call_break_even_long = [strike_price - 2 * premium, strike_price + 2 * premium]
put_call_break_even_short = [strike_price - 2 * premium, strike_price + 2 * premium]
# Ploting put+call payoffs
figure = make_subplots(
rows=1, cols=2, subplot_titles=("Long Put + Long Call", "Short Put + Short Call")
)
figure.add_trace(
go.Scatter(
x=spot_prices, y=put_return_long + call_return_long, name="Long Put + Long Call"
),
row=1,
col=1,
)
figure.add_trace(
go.Scatter(
x=spot_prices,
y=put_return_short + call_return_short,
name="Short Put + Short Call",
),
row=1,
col=2,
)
# Adding breakeven point as circle
figure.add_trace(
go.Scatter(
x=put_call_break_even_long,
y=[0, 0],
mode="markers",
marker=dict(size=10, color="red"),
name="Breakeven for Long Put + Long Call",
),
row=1,
col=1,
)
figure.add_trace(
go.Scatter(
x=put_call_break_even_short,
y=[0, 0],
mode="markers",
marker=dict(size=10, color="red"),
name="Breakeven for Short Put + Short Call",
),
row=1,
col=2,
)
figure.update_layout(
title="Options Payoffs",
xaxis_title="Spot Price",
yaxis_title="Return",
legend_title="Option Type",
font=dict(family="Courier New, monospace", size=18, color="#7f7f7f"),
)
# Setting y axis range
figure.update_yaxes(range=[-premium - 10, premium + 10], row=1, col=1)
figure.update_yaxes(range=[-premium - 10, premium + 10], row=1, col=2)
figure.show()
Put-call parity is an arbitrage between a put price and a stock price that have the same underlying, the same strike price and the same exercise date.
This gives a relationship between the put and call prices. This relationship is called the put-call parity relationship. It is given by:
$$ P-C = K - S + D $$where
The formula means that if you add the present value of the strike price to the sum of put and call, you get the stock price again. (Ignoring dividends)
The put-call parity relation must always hold. If it doesn't, there is an arbitrage opportunity and the market will adjust to make it hold.
On an earlier day, like months or years before the exercise date, the stock option is worth more than the intrinsic value because it has option value. This is so beacuse there's always a chance that the stock will go up.
If you are worried that the stock you have will go down, you can buy a put option. This puts a floor on the price of the stock. If the stock goes down, you can exercise the put option and sell the stock at the strike price. If the stock goes up, you can just hold the stock.